{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典\n",
    "* 字典是另一种可变容器模型，且可存储任意类型对象。\n",
    "* 字典的每个键值(key=>value)对用冒号(:)分割，每个对之间用逗号(,)分割，整个字典包括在花括号({})中\n",
    "> d = {key1 : value1, key2 : value2 }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 111, 2: 222, 's1': 'hello', 's2': 333}\n"
     ]
    }
   ],
   "source": [
    "d1={1:111,2:222,'s1':'hello','s2':333}\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 访问字典里的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 111, 2: 222, 's1': 'hello', 's2': 333}\n",
      "111\n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "print(d1)\n",
    "print(d1[1])\n",
    "print(d1['s1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "333\n"
     ]
    }
   ],
   "source": [
    "print(d1['s2'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 修改字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 'world', 2: 222, 's1': 'hello', 's2': 333, 3: 3333}\n"
     ]
    }
   ],
   "source": [
    "d1[1]='world'\n",
    "d1[3]=3333\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 删除字典元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'd1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-9151f4ddb1fd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0md1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0md1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'd1' is not defined"
     ]
    }
   ],
   "source": [
    "print(d1)\n",
    "del d1[1]\n",
    "print(d1)\n",
    "del d1\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典键的特性\n",
    "* 字典值可以是任何的 python 对象，既可以是标准的对象，也可以是用户定义的，但键不行。\n",
    "* 两个重要的点需要记住：\n",
    "    * 不允许同一个键出现两次。创建时如果同一个键被赋值两次，后一个值会被记住\n",
    "    * 键必须不可变，所以可以用数字，字符串或元组充当，而用列表就不行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 'world', 2: 222, 's1': 'hello', 's2': 333}\n"
     ]
    }
   ],
   "source": [
    "d1={1:111,2:222,'s1':'hello','s2':333}\n",
    "d1[1]='world'\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 'world', 2: 222, 's1': 'hello', 's2': 333, (1, 2): 1111}\n"
     ]
    }
   ],
   "source": [
    "d1[(1,2)]=1111\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-fe5fd312bf72>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0md1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m111\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "d1[[1,2]]=111"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典内置函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 'world', 2: 222, 's1': 'hello', 's2': 333, (1, 2): 1111}\n",
      "5\n",
      "<class 'str'>\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# len 计算字典元素个数\n",
    "print(d1)\n",
    "print(len(d1))\n",
    "# str 输出字典，以可打印的字符串表示\n",
    "print(type(str(d1)))\n",
    "# type 返回变量类型\n",
    "print(type(d1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典内置方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "# clear\n",
    "print(d1)\n",
    "d1.clear()\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 111, 2: 222, 's1': 'hello', 's2': 333}\n"
     ]
    }
   ],
   "source": [
    "# copy\n",
    "d1={1:111,2:222,'s1':'hello','s2':333}\n",
    "d2=d1.copy()\n",
    "print(d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: 11, 1: 11, 2: 11, 3: 11, 4: 11}\n"
     ]
    }
   ],
   "source": [
    "# dict.fromkeys(seq[, value])\n",
    "d2=dict.fromkeys(range(5),11)\n",
    "print(d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 111, 2: 222, 's1': 'hello', 's2': 333}\n",
      "world\n"
     ]
    }
   ],
   "source": [
    "# dict.get(key, default=None)\n",
    "print(d1)\n",
    "print(d1.get(3,'world'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# key in dict\n",
    "print(1 in d1)\n",
    "print(3 in d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([(1, 111), (2, 222), ('s1', 'hello'), ('s2', 333)])\n"
     ]
    }
   ],
   "source": [
    "# # dict.items() 以列表返回可遍历的(键, 值) 元组数组。\n",
    "print(d1.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys([1, 2, 's1', 's2'])\n",
      "<class 'dict_keys'>\n"
     ]
    }
   ],
   "source": [
    "# dict.keys()  以列表返回一个字典所有的键\n",
    "print(d1.keys())\n",
    "print(type(d1.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 111, 2: 222, 's1': 'hello', 's2': 333, 3: None, 4: None}\n"
     ]
    }
   ],
   "source": [
    "# dict.setdefault(key, default=None)  setdefault() 方法和get()方法类似, 如果键不已经存在于字典中，将会添加键并将值设为默认值。\n",
    "print(d1)\n",
    "d1.setdefault(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 111, 2: 222, 's1': 'hello', 's2': 333, 3: None}\n"
     ]
    }
   ],
   "source": [
    "print(d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 111, 2: 222, 's1': 'hello', 's2': 333, 3: None, 4: None}\n"
     ]
    }
   ],
   "source": [
    "# dict.update(dict2) 把字典dict2的键/值对更新到dict里\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: 11, 1: 11, 2: 11, 3: 11, 4: 11}\n"
     ]
    }
   ],
   "source": [
    "print(d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 11, 2: 11, 's1': 'hello', 's2': 333, 3: 11, 4: 11, 0: 11}\n"
     ]
    }
   ],
   "source": [
    "d1.update(d2)\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 11, 2: 11, 's1': 'hello', 's2': 333, 3: 11, 4: 11, 0: 11}\n",
      "dict_values([11, 11, 'hello', 333, 11, 11, 11])\n"
     ]
    }
   ],
   "source": [
    "# dict.values()  以列表返回字典中的所有值\n",
    "print(d1)\n",
    "print(d1.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 11, 2: 11, 's1': 'hello', 's2': 333, 3: 11, 4: 11}\n",
      "world\n",
      "{1: 11, 2: 11, 's1': 'hello', 's2': 333, 3: 11, 4: 11}\n"
     ]
    }
   ],
   "source": [
    "# pop(key[,default])  \n",
    "# 删除字典给定键 key 所对应的值，返回值为被删除的值。key值必须给出。 否则，返回default值。\n",
    "print(d1)\n",
    "print(d1.pop(0,'world'))\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 11, 2: 11, 's1': 'hello', 's2': 333}\n",
      "('s2', 333)\n",
      "{1: 11, 2: 11, 's1': 'hello'}\n"
     ]
    }
   ],
   "source": [
    "# popitem()\n",
    "# 随机返回并删除字典中的一对键和值(一般删除末尾对)。\n",
    "print(d1)\n",
    "print(d1.popitem())\n",
    "print(d1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
